<template>
    <div class="app-container">
        <el-row>
            <el-col :span="24" align="right">
                <el-button type="text" @click="enterCreatePattern" style="font-size: 17px;">新建模式</el-button>
            </el-col>
        </el-row>

        <div>
            <el-table v-loading="listLoading" :data="viewData" element-loading-text="Loading" border fit
                highlight-current-row :cell-style="{ padding: '5px 0' }">
                <el-table-column label="编号" align="center" :min-width="'5%'" :max-width="'5%'">
                    <template slot-scope="scope">
                        {{ scope.row.patternId }}
                    </template>
                </el-table-column>
                <el-table-column label="模式名称" align="center" :min-width="'10%'" :max-width="'10%'">
                    <template slot-scope="scope">
                        {{ scope.row.patternName }}
                    </template>
                </el-table-column>
                <el-table-column label="开始日期" align="center" :min-width="'8%'" :max-width="'8%'">
                    <template slot-scope="scope">
                        {{ scope.row.patternValidStartDate }}
                    </template>
                </el-table-column>
                <el-table-column label="结束日期" align="center" :min-width="'8%'" :max-width="'8%'">
                    <template slot-scope="scope">
                        {{ scope.row.patternValidEndDate }}
                    </template>
                </el-table-column>
                <el-table-column label="上报期限" align="center" :min-width="'7%'" :max-width="'7%'">
                    <template slot-scope="scope">
                        {{ scope.row.patternInvLength }}天
                    </template>
                </el-table-column>
                <el-table-column label="发布间隔" align="center" :min-width="'7%'" :max-width="'7%'">
                    <template slot-scope="scope">
                        {{ scope.row.patternInterval }}天
                    </template>
                </el-table-column>
                <el-table-column label="描述" align="center" :min-width="'30%'" :max-width="'30%'">
                    <template slot-scope="scope">
                        {{ scope.row.patternDescription == null ? "无" : scope.row.patternDescription}}
                    </template>
                </el-table-column>
                <el-table-column label="当前状态" align="center" :min-width="'10%'" :max-width="'10%'">
                    <template slot-scope="scope">
                        {{ scope.row.patternValidStartDate > currentDate ? '未开始' : (
                            scope.row.patternValidEndDate < currentDate ? '已结束' : (scope.row.patternStatus ? '进行中' : '已暂停')) }}
                            </template>
                </el-table-column>
                <el-table-column align="center" label="操作" :min-width="'15%'" :max-width="'15%'">
                    <template slot-scope="scope">
                        <el-button @click="enterModifyPattern(scope.row)" type="text">编辑</el-button>
                        <el-button @click="changePatternStatus(scope.row)" type="text"
                            :disabled="scope.row.patternValidStartDate > currentDate || scope.row.patternValidEndDate < currentDate">{{
                                scope.row.patternStatus ? '暂停' : '开始' }}</el-button>
                        <el-button @click="deletePattern(scope.row)" type="text">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="changePageSize" @current-change="changeCurrentPage"
                :current-page="page.currentPage" :page-sizes="[10, 20, 50]" :page-size="page.pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="page.totalItems">
            </el-pagination>
        </div>

        <el-dialog :title="modifying ? '编辑模式' : '新建模式'" :visible.sync="creating" width="50%">
            <el-form :model="pattern">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="模式名称" label-width="100px">
                            <el-input v-model="pattern.patternName" placeholder="请输入模式名称"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="18">
                        <el-form-item label="模式生效期" label-width="100px">
                            <el-date-picker v-model="pattern.time" type="daterange" unlink-panels range-separator="至"
                                start-placeholder="开始日期" end-placeholder="结束日期" :clearable=false>
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="9">
                        <el-form-item label="上报期限" label-width="100px">
                            <el-input-number v-model="pattern.patternInvLength" :min="1" style="width:100%"
                                size="medium"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="9" :offset="1">
                        <el-form-item label="发布间隔" label-width="80px">
                            <el-input-number v-model="pattern.patternInterval" :min="1" style="width:100%"
                                size="medium"></el-input-number>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="20">
                        <el-form-item label="模式描述" label-width="100px">
                            <el-input v-model="pattern.patternDescription" placeholder="请输入模式描述" type="textarea"
                                :rows="3"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="creating = false">取 消</el-button>
                <el-button type="primary" @click="confirmPattern">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getAllInvestigationPatterns, deleteInvestigationPattern, createInvestigationPattern, modifyInvestigationPattern } from '@/api/province/investigationManage'

export default {
    data() {
        return {
            listLoading: false,
            rawData: [],
            viewData: [],
            currentDate: new Date().toLocaleDateString(),
            page: {
                currentPage: 1,
                pageSize: 10,
                totalItems: 0,
            },
            modifying: false,
            creating: false,
            pattern: {}
        }
    },
    created() {
        this.fetchData();
    },
    methods: {
        display() {
            this.viewData = this.rawData.slice((this.page.currentPage - 1) * this.page.pageSize, Math.min(this.page.totalItems, this.page.currentPage * this.page.pageSize))
        },

        fetchData() {
            this.listLoading = true
            getAllInvestigationPatterns().then(response => {
                response.data.forEach(pattern => {
                    pattern.patternValidStartDate = new Date(pattern.patternValidStartDate).toLocaleDateString()
                    pattern.patternValidEndDate = new Date(pattern.patternValidEndDate).toLocaleDateString()
                })
                this.rawData = response.data
                this.page.totalItems = this.rawData.length
                this.display()
                this.listLoading = false
            })
        },

        changePageSize(val) {
            this.page.pageSize = val;
            this.display();
        },

        changeCurrentPage(val) {
            this.page.currentPage = val;
            this.display();
        },

        deletePattern(pattern) {
            this.$confirm('确定要删除该模式吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteInvestigationPattern({ patternId: pattern.patternId }).then(response => {
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    })
                    this.fetchData()
                })
            }).catch(() =>
                this.$message({
                    type: 'info',
                    message: '已取消'
                })
            )
        },

        changePatternStatus(pattern) {
            let newPattern = {...pattern}
            newPattern.patternValidStartDate = newPattern.patternValidStartDate.replace(/\//g, '-')
            newPattern.patternValidEndDate = newPattern.patternValidEndDate.replace(/\//g, '-')
            newPattern.patternStatus = !newPattern.patternStatus
            modifyInvestigationPattern(newPattern).then(response => {
                this.$message({
                    message: '修改成功',
                    type: 'success'
                })
                pattern.patternStatus = !pattern.patternStatus
            })
        },

        enterCreatePattern() {
            this.creating = true
            this.modifying = false
            this.pattern = { patternInvLength: 7, patternInterval: 30, time: [new Date(), new Date()] }
        },

        enterModifyPattern(pattern) {
            this.creating = true
            this.modifying = true
            this.pattern = { ...pattern, time: [] }
            this.pattern.time = [new Date(this.pattern.patternValidStartDate), new Date(this.pattern.patternValidEndDate)]
        },

        confirmPattern() {
            if (this.pattern.patternName == null || this.pattern.patternName.length == 0)
                this.$message({
                    message: '模式名称不能为空',
                    type: 'warning'
                })
            else {
                this.pattern.patternValidStartDate = this.pattern.time[0].toLocaleDateString().replace(/\//g, '-')
                this.pattern.patternValidEndDate = this.pattern.time[1].toLocaleDateString().replace(/\//g, '-')
                let action = this.modifying ? modifyInvestigationPattern : createInvestigationPattern
                action(this.pattern).then(() => {
                    this.$message({
                        message: (this.modifying ? '修改' : '新建') + '成功',
                        type: 'success'
                    })
                    this.creating = false
                    this.fetchData()
                })
            }
        }
    }
}
</script>